<template>
    <div id="setting">
        <CameraMenu/>
        <MicMenu v-if="micMenuShow"/>
        <SpeakerMenu/>
        <div v-if="isTeacher&&/_121/.test(classMode)" class="set-btn">
          <div @click="doStuContro()">{{studentContro?'禁止':'允许'}}学生控制</div>
        </div>
        <TQuiz v-if="isTeacher"/>
        <Answer v-if="isTeacher"/>
        <!-- <div v-if="isTeacher" class="set-btn">
          <div @click="documentManag()">管理课件</div>
        </div>
        <div v-if="isTeacher" class="set-btn">
          <div @click="documentEmpty()">设置空白</div>
        </div> -->
        <div class="set-btn">
          <div @click="detect()">硬件检测</div>
        </div>
        <div class="set-btn">
          <div @click="handleProtoype()">护眼{{isEyes?'关闭':'开启'}}</div>
        </div>
        <div v-if="isTeacher" class="set-btn">
          <div id="menu-screen-lock"></div>
        </div>
        <!--<div v-if="isClassing" class="set-btn float">
          <div @click="doClassEnd()">下课</div>
        </div>-->
    </div>
    
</template>

<script>
import MicMenu from './MicMenu'
import SpeakerMenu from './SpeakerMenu'
import CameraMenu from './CameraMenu'
import TQuiz from './TQuiz'
import Answer from './Answer'

let eventEmitter = BJY.eventEmitter
let store = window.localStorage
let lockKey = 'yxp_stu_lock'
let ipcRenderer = window.require ? window.require('electron').ipcRenderer : null

export default {
  components: {
    MicMenu,
    SpeakerMenu,
    TQuiz,
    Answer,
    CameraMenu
  },
  data () {
    return {
      studentContro: false,
      hasPad: false,
      classMode: '',
      micMenuShow: false,
      //isClassing: false,
      isTeacher: false,
      isEyes: false
    }
  },
  computed: {},
  watch: {},
  methods: {
    doStuContro () {
      if(!this.isTeacher) return false
      if(window.__syncLock__){
        this.$Toast('正在切换请稍后')
        return false
      }
      this.studentContro = !this.studentContro
      eventEmitter.trigger(eventEmitter.ZWZ_PAD_FORBID, {
        isPadMask: this.studentContro
      })
      setTimeout(() => {
        // 广播事件
        BJY.api.sendBroadcast(
          'YXP_PAD_FORBID',
          {
            isPadMask: !this.studentContro
          }
        )
      }, 1000)
    },
    //doClassEnd () {
      //eventEmitter.trigger(eventEmitter.CLASS_END_TRIGGER)
    //},
    documentManag () {
      eventEmitter.trigger('ZWZ_DOCUMENT')
    },
    // documentEmpty () {
    //   eventEmitter.trigger('ZWZ_DOCUMENT_EMPTY')
    // },
    detect () {
      eventEmitter.trigger('ZWZ_DETECT')
    },
    handleProtoype () {
      this.isEyes = !this.isEyes
      this.$emit('models', this.isEyes)
    }
  },
  created () {
    let that = this
    this.isTeacher = BJY.auth.isTeacher()
    this.classMode = BJY.store.get('yxp_class_mode')
    this.hasPad = BJY.store.get('yxp_has_pad')
    this.micMenuShow = (/_SMALL/i.test(this.classMode) && !this.isTeacher) ? false : true
    if(!this.isTeacher){
      eventEmitter.on(eventEmitter.SCREEN_LOCK, (e, o) => {
        ipcRenderer && ipcRenderer.send('window:lock', o.enable)
      })
    }else{
      // 保存状态
      eventEmitter.on(eventEmitter.SCREEN_LOCK, (e, o) => {
        o.enable ? store.setItem(lockKey, 1) : store.removeItem(lockKey)
      })
    }
    //eventEmitter.on(eventEmitter.CLASS_START, () => {
      //this.isTeacher && (this.isClassing = true)
    //})
    //.on(eventEmitter.CLASS_END, () => {
      //this.isTeacher && (this.isClassing = false)
    //})
    eventEmitter.on(eventEmitter.ZWZ_PAD_FORBID, (e, data) => {
      this.studentContro = data.isPadMask
    })
    // 接收广播事件
    BJY.api.onReceiveBroadcast(
      'YXP_PAD_FORBID',
      function(fromeId, data, cache) {
        // 忽略自己广播信息
        if(fromeId == BJY.store.get('user.id')) return false
        eventEmitter.trigger(eventEmitter.ZWZ_PAD_FORBID, data)
      }
    )
  },
  mounted () {
    // 创建锁屏按钮
    if(this.isTeacher){
      let screenLock = BJY.ScreenLockMenu.create({
        element: $('#menu-screen-lock'),
        onScreenLockClick: (enable) => {
          eventEmitter.trigger(eventEmitter.SCREEN_LOCK_TRIGGER, {
            enable: !enable,
            cache: true
          })
        }
      })
      if(!!store.getItem(lockKey)){
        eventEmitter.trigger(eventEmitter.SCREEN_LOCK_TRIGGER, {enable: true})
      }
      //this.isClassing = BJY.store.get('class.started')
    }
  },
  beforeDestroy () {}
}
</script>

<style lang='less' scoped>
//@import url(); 引入公共css类
#setting {
  display: flex;
  align-items: center;
}
.set-btn{
  margin-left: 20px;
  cursor: pointer;
  .float{
    float:right;
  }
}
</style>
